from fastapi import FastAPI
from fastapi.requests import Request
import json

def getParams( req: Request):
    res ={}
    try:
        ps= {}
        for k,v in req.query_params.items():
            ps[k] = v
        def assignPs( parent , k , v ):
            pos = k.find("$")
            if pos<0:
                parent[k] = v
            else:
                pk=k[:pos]
                sk = k[pos+1:]
                if pk not in parent.keys():
                    parent[pk]={}
                np = parent[pk]
                assignPs(np , sk , v )
        for k,v in ps.items():
            assignPs(res , k , v)
    except Exception as  er:
        print(er)
    return res
def getJsonParams( req: Request):
    res ={}
    try:
        ps= {}
        for k,v in req.query_params.items():
            ps[k] = v
        for k , v in ps.items():
            pos = k.find("$json")
            if pos>0:
                try:
                    objK = k[:pos]
                    res[objK] = json.loads(v)
                except Exception as eer:
                    print(eer)
            else:
                res[k] = v
    except Exception as  er:
        print(er)
    return res